﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace JinfuyuanProject.Repository
{
    public class SalaryRepository:ISalaryRepository
    {
        private readonly JinfuyuanDbContext _db;

        public SalaryRepository(JinfuyuanDbContext db)
        {
            _db = db;
        }
        #region 基础方法
        public IQueryable<Salary> GetList()
        {
            try
            {
                return _db.Salary;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool Delete(Salary Salary)
        {
            try
            {

                _db.Entry(Salary).State = System.Data.Entity.EntityState.Deleted;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                //记录异常日志
                return false;
            }
        }
        public Salary GetModel(int Id)
        {
            try
            {
                Salary Salary = _db.Salary.Find(Id);
                return Salary;
            }
            catch (Exception ex)
            {
                return null;
            }

        }

        public int Insert(Salary Salary)
        {
            try
            {
                _db.Entry(Salary).State = System.Data.Entity.EntityState.Added;
                _db.SaveChanges();
                return Salary.Id;
            }
            catch (Exception ex)
            {
                return 0;
            }

        }

        public bool Update(Salary Salary)
        {
            try
            {

                _db.Entry(Salary).State = System.Data.Entity.EntityState.Modified;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion
        #region 扩展方法
        // 获取从上个月15号到本月15号的所有员工薪资信息
        public async Task<List<Salary>> GetSalariesAsync(DateTime startDate, DateTime endDate)
        {
            // 假设你有一个数据库上下文 _dbContext 进行查询
            return await _db.Salary
                .Where(s => s.CreateTime >= startDate && s.CreateTime <endDate)
                .ToListAsync();
        }
        // 保存薪资信息
        public async Task SaveSalaryAsync(Salary salary)
        {
            _db.Entry(salary).State = System.Data.Entity.EntityState.Modified;
            await _db.SaveChangesAsync();
        }
        #endregion
    }
}
